package com.heima.user.filter;

import com.heima.model.user.pojo.ApUser;
import com.heima.utils.threadlocal.ApThreadLocalUtils;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author Ronaldo
 * @date 2021/8/3
 */

@Order(1)
@Component
@WebFilter(filterName = "apTokenFilter",urlPatterns = "/*")
public class ApTokenFilter extends GenericFilter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;

        String userId = request.getHeader("userId");
        if(userId != null){
            ApUser apuser = new ApUser();
            apuser.setId(Integer.valueOf(userId));
            ApThreadLocalUtils.setUser(apuser);
        }

        // 如果没有则直接放行
        filterChain.doFilter(request,response);
    }
}
